<script type="text/html" template lay-done="layui.data.permanentUpdatedone(d);">
    <div class='permanent-updateHtml-content'>
        <div class="layui-card card-content">
            <div class="layui-card-body main-box">
                <div class="layui-innerbox" style="background-color: #fff;">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <form class="layui-form permanent-updateHtml-form" action="" lay-filter="permanent-updateHtml-form-group">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">任务名称：</label>
                                    <div class="layui-input-inline" style="margin-left:23px;">
                                        <input type="text" class="layui-hide" name="id" value='{{d.params.id ? d.params.id : ""}}'>
                                        <input type="text" value='{{d.params.taskname ? d.params.taskname : ""}}' name="taskname" lay-verify="taskname" autocomplete="off" placeholder="请输入标题" class="layui-input">
                                    </div>
                                    <span class="mustred">*</span>
                                </div>
                                <div class="layui-form-item" style="margin-top: 10px;">
                                    <div class="layui-form-item layui-form-text">
                                        <label class="layui-form-label">填报要求：</label>
                                        <div class="layui-input-block">
                                            <textarea name="memo" placeholder="请输入内容" class="layui-textarea" style="min-height: 112px;">
                                                    {{d.params.memo ? d.params.memo : ""}}
                                            </textarea>
                                        </div>
                                    </div>
                                    <div class="special-content">
                                        <div class="special-key">
                                            <form class="dropzone" id="permanent-upload-filie" style="display:none;">
                                                <div class="fallback" style="display:none;">
                                                    <input type="file" name="file" multiple />
                                                </div>
                                            </form>   
                                            <button type="button" class="layui-btn layui-btn-xs" id="myAwesomeDropzone"><i class="layui-icon"></i>附件</button>                                
                                            <!-- 原生上传文件 -->
                                            <!-- <a href="javascript:;" class="file gradient">
                                                <i class="layui-icon"></i><input type="file" id="uploadFile">附件
                                            </a> -->
                                            <!-- upload组件 -->
                                            <!-- <button type="button" class="layui-btn layui-btn-xs" id="permanent-upload-filie"><i class="layui-icon"></i>附件</button> -->
                                        </div>
                                        <div class="special-value">
                                            <div class="check-value-box permanent-update-files"></div>
                                        </div>
                                    </div>
                                    <div class="special-content">
                                        <div class="special-key">
                                            <button type="button" class="layui-btn layui-btn-xs T-event unit" data-target="unit"><i class="layui-icon">&#xe608;</i>下发单位</button>
                                        </div>
                                        <div class="special-value">
                                            <div class="check-value-box permanent-update-department">
                                                    {{#
                                                        if(d.params.deptMsg || d.params.groupMsg) {
                                                            var exitDep = d.params.deptMsg.length ? d.params.deptMsg : [],
                                                                exGgrp = d.params.groupMsg.length ? d.params.groupMsg : [],
                                                                allEx = exitDep.concat(exGgrp);
                                                            layui.each(allEx, function(index,item) {
                                                    }}
                                                        <div class="unit-content">
                                                            {{item.writeusername}}
                                                            <i class="layui-icon layui-icon-close T-event unit" data-target="delUnit" data-deptid="{{item.dataid}}"></i>
                                                        </div>
                                                    {{#
                                                            })
                                                        }
                                                    }}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="layui-col-md6">
                            <form class="layui-form permanent-frequency-form" action="" lay-filter="permanent-frequency-form">
                                <div class="layui-form-item">
                                    <div style="display:none;">
                                        <label class="layui-form-label" style="text-align:left;">时间设置：</label>
                                        <div class="layui-input-block">
                                            <div class="leftType">
                                                <div class="topLeftType">
                                                    <input type="radio" name="timeType" value="frequency" title="固定频率" lay-filter="timeType" {{ d.params.settype && d.params.settype === '0' ? "checked" : null}}>
                                                </div>
                                                <div class="bottomLeftType">
                                                    <input type="radio" name="timeType" value="time" title="固定时间" lay-filter="timeType" {{ d.params.settype && d.params.settype === '1' ? "checked" : null}}>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="frequencyType">
                                        <div class="frequencyTypeDiv">
                                            <input type="radio" name="frequencyType" lay-filter="frequencyTimeType" value="year" title="每年" {{ d.params.settype && d.params.settype === '1' ? "disabled" : null}} {{ d.params.hz && d.params.hz === 'year' ? "checked" : null}}>
                                        </div>
                                        <div class="frequencyTypeDiv">
                                            <input type="radio" name="frequencyType" lay-filter="frequencyTimeType" value="quarter" title="每季度" {{ d.params.settype && d.params.settype === '1' ? "disabled" : null}} {{ d.params.hz && d.params.hz === 'quarter' ? "checked" : null}}>
                                        </div>
                                        <div class="frequencyTypeDiv">
                                            <input type="radio" name="frequencyType" lay-filter="frequencyTimeType" value="month" title="每月" {{ d.params.settype && d.params.settype === '1' ? "disabled" : null}} {{ d.params.hz && d.params.hz === 'month' ? "checked" : null}}>
                                        </div>
                                        <div class="frequencyTypeDiv">
                                            <input type="radio" name="frequencyType" lay-filter="frequencyTimeType" value="week" title="每周" {{ d.params.settype && d.params.settype === '1' ? "disabled" : null}} {{ d.params.hz && d.params.hz === 'week' ? "checked" : null}}>
                                        </div>
                                        <div class="frequencyTypeDiv">
                                            <input type="radio" name="frequencyType" lay-filter="frequencyTimeType" value="day" title="每日" {{ d.params.settype && d.params.settype === '1' ? "disabled" : null}} {{ d.params.hz && d.params.hz === 'day' ? "checked" : null}}>
                                        </div>
                                    </div>
                                    <div class="layui-input-block fill-time-block" style="margin-top: 5px;">
                                        <div class="layui-form-item" style="padding: 10px 0px 0px 10px;">
                                            <div class="dateType" lay-filter="frequencyDateType"></div>
                                            <div class="listType">
                                                <div class="header">
                                                    最近5次填报时间
                                                    <button type="button" style="float: right;" class="layui-btn layui-btn-sm T-event" data-target="viewlast">查看</button>
                                                </div>
                                                <div class="body"></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="layui-collapse">
                    <div class="layui-colla-item">
                        <h2 class="layui-colla-title c-iframeTitle" style="height:24px;line-height:24px;overflow: visible;">表单</h2>
                        <div class="layui-colla-content layui-show iframeContent">
                            <iframe id="mainFormIframeInPermanent" class="permanent-updateHtml-form" src="./TemForm/permanent.html" frameborder="0"></iframe>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="buttons-area">
            <button type="button" class="T-event layui-btn" data-target="add">{{d.params.id ? '修改' : '创建'}}任务</button>
        </div>
    </div>
</script>
<style>
.permanent-updateHtml-content  .c-iframeTitle {
    height:24px;
    line-height:24px;
    -moz-user-select:none;
    -ms-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    overflow: visible;
}
.permanent-updateHtml-content {
    height: 100%;
    overflow: hidden;
}
.permanent-updateHtml-content .card-content {
	height: 89%;
    overflow: auto;
    background-color: #fff;
}
.permanent-updateHtml-content .layui-form-item{
    margin: 0;
}
.layui-innerbox{
    border: 1px solid #D2D2D2;
    border-radius: 4px;
    padding-top: 17px;
}
.permanent-updateHtml-content .layui-form-item .layui-inline {
    margin-right: 0px;
    width: 49%;
}
.permanent-updateHtml-content .mustred{
    display: block;
    color: red;
    line-height: 45px;
    height: 0px;
}
.permanent-updateHtml-content .layui-form-item .layui-input-inline {
    width: 67%;
    margin-right: 5px;
}
.permanent-updateHtml-content .layui-input-block{
    width: 67%;
}
.permanent-updateHtml-content .fill-time-block {
    border: 1px solid rgb(167,198,238);
    border-radius: 6px;
    width: 100% !important;
    height: auto;
    margin: 0;
}
.permanent-updateHtml-content .layui-form-switch {
    background: #D2D2D2;
}
.permanent-updateHtml-content .layui-form-switch em {
    margin-left: 28px;
    color: #fff!important;
}
.permanent-updateHtml-content .layui-form-onswitch em {
    margin-left: 9px !important;
}
.permanent-updateHtml-content .layui-form-switch i {
    background-color: #fff;
}
.permanent-updateHtml-content .layui-form-onswitch i {
    margin-left: -22px;
}
.permanent-updateHtml-content .layui-btn-xs {
    height: 38px;
    background: #2669B1;
    margin-top: -8px;
    border-radius: 5px;
    width: 90px;
    margin-left: 5px;
}
.permanent-updateHtml-content .special-value {
    overflow: hidden;
    border: 1px solid #ccc;
    height: 38px;
    width: 67%;
    border-radius: 6px;
    border: 1px solid rgb(197,197,197);
}
.permanent-updateHtml-content .layui-input, .layui-textarea{
    border-radius: 6px;
    border: 1px solid rgb(197,197,197);
}
.permanent-updateHtml-content .layui-form-onswitch{
    border-color: #2669B1;
    background-color: #2669B1;
}
.permanent-updateHtml-content .special-content {
    width: 100%;
    margin-top: 26px;
}
.permanent-updateHtml-content .special-key {
    overflow: hidden;
    width: 96px;
    float: left;
    padding: 9px 3px;
    padding-right: 10px;
    text-align: right;
}
.permanent-updateHtml-content .iframeContent {
    width: 100%;
    height: 475px;
    overflow: hidden;
    padding:0px;
}
.main-box{
    padding: 15px;
}
.permanent-updateHtml-content .iframeContent .permanent-updateHtml-form {
    width: 99%;
    height: 100%;
    overflow: auto;
}
.permanent-updateHtml-form{
    background-color: #FFFFFF;
}
.permanent-updateHtml-content .buttons-area {
	height: 40px;
	text-align: center;
    background-color: #FFFFFF;
}
.buttons-area button{
    background-color: #2669B1;
}
.permanent-updateHtml-content .check-value-box {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    padding: 2px 2px;
}
.permanent-updateHtml-content .unit-content {
    display: inline-block;
    padding: 2px 5px;
    padding-right: 15px;
    background-color: #ccc;
    position: relative;
    height: 20px;
    line-height: 20px;
}

.permanent-updateHtml-content .unit-content:hover i{
    visibility: visible;
    color: red;
    font-weight: 600;
}

.permanent-updateHtml-content .unit-content i {
    position: absolute;
    right: 0px;
    top: -2px;
    font-size: 14px;
    cursor: pointer;
    visibility: hidden;
}
.permanent-updateHtml-content .layui-form-radio {
    margin: 6px 0px 0 0px;
}
.permanent-updateHtml-content .layui-form-radio>i:hover, .layui-form-radioed>i {
    color: #276AB1;
}
.permanent-updateHtml-content .layui-input-block .leftType {
    width: 100%;
    height: 40px;
    display: flex;
}
.permanent-updateHtml-content .frequencyType {
    width: 100%;
    margin: 0 auto;
    height: 40px;
    border-bottom: 1px rgb(210,210,210) solid;
}
.permanent-updateHtml-content .frequencyTypeDiv{
    float: left;
    width: 20%;
    height: 40px;
}
.permanent-frequency-form{
    width: 99%;
    margin: 6px 0px 0px 0px;
}
.permanent-frequency-form .dateType {
    width: 65%;
    height: 100%;
    min-height: 187px;
    float: left;
    border-right: 1px solid #ccc;
}
.permanent-frequency-form .date-item {
    width: 100%;
    height: 32px;
    clear: both;
}
.permanent-frequency-form .dateType .layui-btn-sm{
    height: 30px;
    line-height: 23px;
    padding: 0px 6px 0px 8px;
    font-size: 12px;
    border: 1px #009688 solid;
    background-color: transparent;
    border-radius: 6px;
    color: #009688;
    margin-right: 5px;
}
.permanent-frequency-form .listType .layui-btn-sm{
    height: 30px;
    line-height: 23px;
    padding: 0px 6px 0px 8px;
    font-size: 12px;
    border: 1px #009688 solid;
    background-color: transparent;
    border-radius: 6px;
    color: #009688;
}
.permanent-frequency-form .date-item input {
    line-height: 32px;
}
.permanent-frequency-form .dateType .layui-form-mid {
    padding: 7px 0px !important;
}
.permanent-frequency-form .date-item .layui-input {
    height: 30px;
}
.permanent-frequency-form .listType {
    width: 34%;
    float: left;
    height: 100%;
}
.permanent-frequency-form .listType .header {
    height: 30px;
    text-align: center;
    line-height: 30px;
    font-size: 14px;
}
.permanent-frequency-form .listType .body {
    height: 157px;
    overflow-y: auto;
    text-align: center;
}
.layui-layer{
	border-radius: 8px;
}
.layui-form-label{
	padding: 8px 3px;
}
.layui-layer-setwin{
    top: 10px;
    right: 11px;
}
.layui-layer-setwin .layui-layer-close1{
    background-position: 0 0;
}
.layui-layer-setwin a{
    width: 18px;
    height: 18px;
    margin: 0;
    background: url('/src/style/res/formFill/layer-close.png') no-repeat center center;
    background-position: 0px 0px;
}
.permanent-updateHtml-content .layui-form-item .layui-inline .layui-review-box span{
    display: inline-block;
    height: 40px;
    line-height: 40px;
    color: #666;
    cursor: pointer;
    vertical-align: top;
}
.to-verify{
    margin-left: 78px;
}
.review-value-box, .audit-value-box{
    width: 225px;
    height: 35px;
	line-height: 20px;
	border-radius: 6px;
	margin-left: 10px;
	overflow-y: scroll;
	border: 1px solid #cccccc;
}
.layui-collapse{
    margin-top: 10px;
}
.permanent-updateHtml-content .file {
    position: relative;
    display: inline-block;
    overflow: hidden;
    text-decoration: none;
    text-indent: 0;
    height: 38px;
    background: #2669B1;
    margin-top: -8px;
    border-radius: 5px;
    width: 90px;
    text-align: center;
    line-height: 38px;
    color: #fff;
}
.file input {
    position: absolute;
    font-size: 100px;
    width: 90px;
    height: 38px;
    right: 0;
    top: 0;
    opacity: 0;
}

.gradient{
    filter:alpha(opacity=100 finishopacity=50 style=1 startx=0,starty=0,finishx=0,finishy=150) progid:DXImageTransform.Microsoft.gradient(startcolorstr=#fff,endcolorstr=#ccc,gradientType=0);
    -ms-filter:alpha(opacity=100 finishopacity=50 style=1 startx=0,starty=0,finishx=0,finishy=150) progid:DXImageTransform.Microsoft.gradient(startcolorstr=#fff,endcolorstr=#ccc,gradientType=0);/*IE8*/    
    background:#ccc; /* 一些不支持背景渐变的浏览器 */  
    background:-moz-linear-gradient(top, #fff, #ccc);  
    background:-webkit-gradient(linear, 0 0, 0 bottom, from(#fff), to(#ccc));  
    background:-o-linear-gradient(top, #fff, #ccc); 
}
#addItem{
    display: none;
}
</style>
<script type="text/html" id="permanentTimeItemTpl">
    {{# 
        var isFirst=d.isFirst;
    }}
    <div class="date-item">
        <div class="layui-input-inline" style="width: 135px;">
            <input type="text" name="starttime" value="{{d.data.starttime || ''}}" class="layui-input item-time item-time-start" readonly data-timetype="start" placeholder="选择时间">
        </div>
        <div class="layui-input-inline" style="width: 5px;">-</div>
        <div class="layui-input-inline" style="width: 135px;">
            <input type="text" name="endtime" value="{{d.data.endtime || ''}}" class="layui-input item-time item-time-end" readonly data-timetype="end" placeholder="选择时间">
        </div>
        {{# if(!!isFirst) { }}
        <button type="button" id="addItem" class="layui-btn layui-btn-sm T-bEvent" data-target="addItem">
            <i class="layui-icon layui-icon-add-1"></i>
        </button>
        {{# } else {  }}
        <button type="button" class="layui-btn layui-btn-sm T-bEvent" data-target="delItem">
            <i class="layui-icon layui-icon-close"></i>
        </button>
        {{# } }}
    </div>
</script>
<script type="text/html" id="permanentselectunitTpl">
    {{#
        layui.each(d, function(index,item) {
    }}
        <div class="unit-content">
            {{item.writeusername}}
            <i class="layui-icon layui-icon-close T-event unit" data-target="delUnit" data-deptid="{{item.dataid}}"></i>
        </div>
    {{#
        })
    }}
</script>
<script type="text/html" id="permanentselectgroupTpl">
    {{#
        layui.each(d, function(index,item) {
    }}
        <div class="unit-content">
            {{item.writeusername}}
            <i class="layui-icon layui-icon-close T-event group" data-target="delUnit" data-deptid="{{item.dept_id}}"></i>
        </div>
    {{#
        })
    }}
</script>
<script type="text/html" id="permanentShowLastTpl">
    {{#
        layui.each(d, function(index,item) {
    }}
        <div style="background-color:#ccc;border-radius: 6px;margin-top: 5px;">
            <p>开始：{{item.starttime}}</p>
            <p>结束：{{item.endtime}}</p>
        </div>
    {{#
        })
    }}
</script>
<script type="text/html" id="permanentuploadFilesTpl">
    {{#
        layui.each(d, function(index,item) {
    }}
        <div class="unit-content">
            <a class="T-event" data-target="download" href="javascript: void(0);" data-url="{{item.url}}">{{item.filename}}</a>
            <i class="layui-icon layui-icon-close T-event" data-target="delFile" data-deptid="{{item.servername}}"></i>
        </div>
    {{#
        })
    }}
</script>
<script>
layui.data.permanentUpdatedone = function (d) { 
    layui.use(['admin', 'form', 'laydate', 'upload', 'tools'], function(){
        var $ = layui.$,
        admin = layui.admin,
        view = layui.view,
        element = layui.element,
        layer = layui.layer,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        upload = layui.upload,
        setter = layui.setter,
        tools = layui.tools,
        form = layui.form,
        filenames = [],
        timeInputType = 'year', // year, quarter, month, week ,day
        switchKey = parseInt(d.params.review) || 0,
        uploadFileUrl = "myapi/DC/api/rest/web/custom/uploadfile";
        
        form.render(null, 'permanent-updateHtml-form-group');
        form.render(null, 'permanent-frequency-form');

        element.render();

        // var tempFileName = '';
        // upload.render({
        //     elem: '#permanent-upload-filie',
        //     accept: 'file',
        //     url: 'myapi/DC/api/rest/web/custom/uploadfile?access_token='+layui.data('layuiAdmin').access_token,
        //     choose: function(obj) {
        //         obj.preview(function(index, file, result){
        //             tempFileName = file.name;
        //         });
        //     },
        //     done: function(res, index, upload){ //上传后的回调
        //         if(res.retcode == 0) {
        //             filenames.push({filename: tempFileName, servername: res.servername, url: res.url});
        //             var getTpl = permanentuploadFilesTpl.innerHTML,
        //                 $tplC =$('.permanent-update-files');
        //             laytpl(getTpl).render(filenames, function(html){
        //                 $tplC.html(html);
        //             });
        //         }else {
        //             layer.msg('提示: '+ res.retmsg,{time: 1000});
        //         }
        //     },
        //     error: function(index, upload){
        //         layer.msg('上传文件过大，请重新选择文件上传', {time: 1000});
        //     }
        // });

        //监听文件改变
        // $(".file").on("change","input[type='file']",function(e){
        //     var element = document.getElementById('uploadFile');
        //     tools.submitUpload(element, 'myapi/DC/api/rest/web/custom/uploadfile', success = function(res){
        //         filenames.push({filename: res.filename, servername: res.servername, url: res.url});
        //         var getTpl = permanentuploadFilesTpl.innerHTML,
        //             $tplC =$('.permanent-update-files');
        //         laytpl(getTpl).render(filenames, function(html){
        //             $tplC.html(html);
        //         });
        //     });
        // });

        //dropzone文件上传
        tools.DropzoneUpload("#myAwesomeDropzone", uploadFileUrl, {}, success = function(res){
            filenames.push({filename: res.filename, servername: res.servername, url: res.url});
            var getTpl = permanentuploadFilesTpl.innerHTML,
                $tplC =$('.permanent-update-files');
            laytpl(getTpl).render(filenames, function(html){
                $tplC.html(html);
            });
        });
        
        if(d.params.id) { //修改
            var timeDate = d.params.timeDate && d.params.timeDate.length && d.params.timeDate.length > 0 ? d.params.timeDate : [];
            if(timeDate.length > 0) {
                $.each(timeDate, function(index, item) {
                    item.starttime = moment(item.starttime).format('YYYY-MM-DD HH:mm');
                    item.endtime = moment(item.endtime).format('YYYY-MM-DD HH:mm');
                    insertDateTplByType(index === 0, item);
                })
            }else {
                insertDateTplByType(true);
            }
            var exitFileNames = d.params.fileNames;
            if(exitFileNames.length > 0) {
                filenames = exitFileNames;
                var Tplhtml = permanentuploadFilesTpl.innerHTML;
                var $tplF =$('.permanent-update-files');
                laytpl(Tplhtml).render(exitFileNames, function(html){
                    $tplF.html(html);
                });
            }
        }else { //新增
            insertDateTplByType(true);
        }
        
        //表单中时间频率类型改变事件
        form.on('radio(timeType)', function(data){
            var $form = $('.permanent-updateHtml-content .permanent-frequency-form');
            var timeTypeValue = data.value;
            if(timeTypeValue === 'frequency') {
                $form.find('.leftType input[name="timeType"][value="frequency"]').attr('checked', true);
                $form.find('.leftType input[name="timeType"][value="time"]').attr('checked', false);
                $form.find('.frequencyType input:radio').each(function(i, el) {
                    $(el).attr('disabled', false);
                    if(i===0) {
                        $(el).attr('checked', true);
                        $(el).click();
                    }
                });
                form.render('radio', 'permanent-frequency-form');
            }else if(timeTypeValue === 'time') {
                $form.find('.leftType input[name="timeType"][value="time"]').attr('checked', true);
                $form.find('.leftType input[name="timeType"][value="frequency"]').attr('checked', false);
                $form.find('.frequencyType input:radio').each(function(i, el) {
                    $(el).attr('disabled', true);
                })
                $form.find('input[name="frequencyType"]').removeAttr('checked');
                form.render('radio', 'permanent-frequency-form');
            }
            timeInputType = 'year';
            insertDateTplByType(true);
        });

        //表单中时间频率类型改变事件
        form.on('radio(frequencyTimeType)', function(data){
            var $form = $('.permanent-updateHtml-content .permanent-frequency-form');
            var frequencyTimeTypeValue = data.value;
            timeInputType = frequencyTimeTypeValue;
            insertDateTplByType(true);
        });

        //频率类型为频率时，根据时间来插入不同的时间选择组件
        function insertDateTplByType(isFirst,values) {
            var getTpl = permanentTimeItemTpl.innerHTML;
            var $tplC =$('.permanent-frequency-form .dateType');
            laytpl(getTpl).render({isFirst: isFirst, data: !!values ? values : {} }, function(html){
                var $ItemHtml = $(html);
                //给第一个时间行绑定时间组件
                bindTypeForItem($ItemHtml);
                !!isFirst ? $tplC.html($ItemHtml) : $tplC.append($ItemHtml);
                form.render(null, 'permanent-frequency-form');
            });
        }

        //监听开关
	    form.on('switch(layui-review-switch)', function(data){
		    if(data.elem.checked){
				switchKey = 1;
				$('.review-value-box').removeClass('layui-bg-gray');
			}else{
				switchKey = 0;
				$('.review-value-box').empty().addClass('layui-bg-gray');
			}
        });
        
        //监听任务名称输入框
        $('input[name="taskname"]').on('input propertychange', function(){
            var str = $(this).val(),
                length = tools.GetStrLength(str);
            if(length > 256){
                $('input[name="taskname"]').val(str.substr(0, 256));
                layer.msg('任务名称长度不能超过256');
            }
        });

        //监听填报要求输入框
        $('textarea[name="memo"]').on('input propertychange', function(){
            var str = $(this).val(),
                length = tools.GetStrLength(str);
            if(length > 2048){
                $('input[name="memo"]').val(str.substr(0, 2048));
                layer.msg('填报要求长度不能超过2048');
            }
        });



        //给时间行绑定相应的时间操作
        function bindTypeForItem($ItemContariner, times) {
            $ItemContariner.each(function(index, el) {
                var commonMin = { year: 1900, month: 0, date: 1},
                    commonMax = { year: 2099, month: 11, date: 31};
                var startVal = $ItemContariner.find('.item-time-start').eq(0).val();
                    endVal =$ItemContariner.find('.item-time-end').eq(0).val();
                switch(timeInputType) {
                    case 'year':
                    var startDate = laydate.render({
                        elem: $ItemContariner.find('.item-time-start').get(0),
                        min: tools.minDate('date'),
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        done: function (value, date) {
                            if(!!value) {
                                endDate.config.min = {
                                    year:date.year,
                                    month:date.month-1, 
                                    date: date.date,
                                };
                                endDate.config.max ={
                                    year: date.year+1,
                                    month:date.month-1, 
                                    date: date.date,
                                };
                            }else {
                                endDate.config.min = commonMin;
                                endDate.config.max = commonMax;
                            }
                        }
                    });
                    var endDate = laydate.render({
                        elem: $ItemContariner.find('.item-time-end').get(0),
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        min: !!startVal ? startVal : '1900-00-01',
                        done: function (value, date) {
                            if(!!value) { 
                                startDate.config.min = {
                                    year: date.year-1,
                                    month: date.month - 1,
                                    date: date.date,
                                } 
                                startDate.config.max = {
                                    year: date.year,
                                    month: date.month - 1,
                                    date: date.date,
                                } 
                            }else {
                                startDate.config.min = commonMin;
                                startDate.config.max = commonMax; 
                            }
                        }
                    });
                    break;
                    case 'quarter':
                    var startDate = laydate.render({
                        elem: $ItemContariner.find('.item-time-start').get(0),
                        min: tools.minDate('date'),
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        done: function (value, date) {
                            if(!!value) {
                                endDate.config.min = {
                                    year:date.year,
                                    month:date.month-1, 
                                    date: date.date,
                                };
                                var newDate = moment(value).add(3, 'M');
                                endDate.config.max ={
                                    year: newDate.year(),
                                    month: newDate.month(),
                                    date: newDate.date(),
                                };
                            }else {
                                endDate.config.min = commonMin;
                                endDate.config.max = commonMax;
                            }
                        }
                    });
                    var endDate = laydate.render({
                        elem: $ItemContariner.find('.item-time-end').get(0),
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        min: !!startVal ? startVal : '1900-00-01',
                        done: function (value, date) {
                            if(!!value) {
                                var newDate = moment(value).subtract(3, 'M');
                                startDate.config.min = {
                                    year: newDate.year(),
                                    month: newDate.month(),
                                    date: newDate.date(),
                                } 
                                startDate.config.max = {
                                    year: date.year,
                                    month: date.month - 1,
                                    date: date.date,
                                } 
                            }else {
                                startDate.config.min = commonMin;
                                startDate.config.max = commonMax; 
                            }
                        }
                    });
                    break;
                    case 'month':
                    var startDate = laydate.render({
                        elem: $ItemContariner.find('.item-time-start').get(0),
                        min: tools.minDate('date'),
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        done: function (value, date) {
                            if(!!value) {
                                endDate.config.min = {
                                    year:date.year,
                                    month:date.month-1, 
                                    date: date.date,
                                };
                                var newDate = moment(value).add(1, 'M');
                                endDate.config.max ={
                                    year: newDate.year(),
                                    month: newDate.month(),
                                    date: newDate.date(),
                                };
                            }else {
                                endDate.config.min = commonMin;
                                endDate.config.max = commonMax;
                            }
                        }
                    });
                    var endDate = laydate.render({
                        elem: $ItemContariner.find('.item-time-end').get(0),
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        min: !!startVal ? startVal : '1900-00-01',
                        done: function (value, date) {
                            if(!!value) {
                                var newDate = moment(value).subtract(1, 'M');
                                startDate.config.min = {
                                    year: newDate.year(),
                                    month: newDate.month(),
                                    date: newDate.date(),
                                } 
                                startDate.config.max = {
                                    year: date.year,
                                    month: date.month - 1,
                                    date: date.date,
                                } 
                            }else {
                                startDate.config.min = commonMin;
                                startDate.config.max = commonMax; 
                            }
                        }
                    });
                    break;
                    case 'week':
                        var startDate = laydate.render({
                            elem: $ItemContariner.find('.item-time-start').get(0),
                            min: tools.minDate('date'),
                            type: 'datetime',
                            format: 'yyyy-MM-dd HH:mm',
                            done: function (value, date) {
                                if(!!value) {
                                    endDate.config.min = {
                                        year:date.year,
                                        month:date.month-1, 
                                        date: date.date,
                                    };
                                    var newDate = moment(value).add(1, 'w');
                                    endDate.config.max ={
                                        year: newDate.year(),
                                        month: newDate.month(),
                                        date: newDate.date(),
                                    };
                                }else {
                                    endDate.config.min = commonMin;
                                    endDate.config.max = commonMax;
                                }
                            }
                        });
                        var endDate = laydate.render({
                            elem: $ItemContariner.find('.item-time-end').get(0),
                            type: 'datetime',
                            format: 'yyyy-MM-dd HH:mm',
                            min: !!startVal ? startVal : '1900-00-01',
                            done: function (value, date) {
                                if(!!value) {
                                    var newDate = moment(value).subtract(1, 'w');
                                    startDate.config.min = {
                                        year: newDate.year(),
                                        month: newDate.month(),
                                        date: newDate.date(),
                                    } 
                                    startDate.config.max = {
                                        year: date.year,
                                        month: date.month - 1,
                                        date: date.date,
                                    } 
                                }else {
                                    startDate.config.min = commonMin;
                                    startDate.config.max = commonMax; 
                                }
                            }
                        });
                    break;
                    case 'day':
                    var startDate = laydate.render({
                        elem: $ItemContariner.find('.item-time-start').get(0),
                        min: tools.minDate('datetime'),
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        done: function (value, date) {
                            if(!!value) {
                                endDate.config.min = {
                                    year:date.year,
                                    month:date.month-1, 
                                    date: date.date,
                                    hours: date.hours,
                                    minutes: 0,
                                    seconds: 0
                                };
                                var newDate = moment(value).add(1, 'd');
                                endDate.config.max ={
                                    year: newDate.year(),
                                    month: newDate.month(),
                                    date: newDate.date(),
                                    hours: newDate.hours(),
                                    minutes: 0,
                                    seconds: 0
                                };
                            }else {
                                endDate.config.min = commonMin;
                                endDate.config.max = commonMax;
                            }
                        }
                    });
                    var endDate = laydate.render({
                        elem: $ItemContariner.find('.item-time-end').get(0),
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        min: !!startVal ? startVal : '1900-00-01',
                        done: function (value, date) {
                            if(!!value) {
                                var newDate = moment(value).subtract(1, 'd');
                                startDate.config.min = {
                                    year: newDate.year(),
                                    month: newDate.month(),
                                    date: newDate.date(),
                                    hours: newDate.hours(),
                                    minutes: 0,
                                    seconds: 0
                                } 
                                startDate.config.max = {
                                    year: date.year,
                                    month: date.month - 1,
                                    date: date.date,
                                    hours: date.hours,
                                    minutes: 0,
                                    seconds: 0
                                } 
                            }else {
                                startDate.config.min = commonMin;
                                startDate.config.max = commonMax; 
                            }
                        }
                    });
                    break;
                    default:
                    break;
                }
                
            })
        }

        var selectDep = d.params.deptMsg || [],
            selectGrp = d.params.groupMsg || [],
            originDep = [],
            originGrp = [];
            $.each(selectDep, function(i,e) {
                e.dept_id = e.dataid;
                originDep.push(e);
            });
            $.each(selectGrp, function(i,e) {
                e.dept_id = e.dataid;
                originGrp.push(e);
            });
        window.selectTable = d.params.formMsg || [];

        //时间频率相关操作
        $('.permanent-updateHtml-content').on('click', '.T-bEvent', function() {
            var target = $(this).data('target');
            switch(target) {
                case 'addItem':
                    var itemLength = $(this).closest('.dateType').find('.date-item').length;
                    if(itemLength === 5) {
                        return false;
                    }
                    insertDateTplByType(false);
                break;
                case 'delItem':
                    var $item = $(this).closest('.date-item');
                    $item.remove();
                break;
                default:
                break;
            }
        })

        //加载编辑时已选择的表单
        var mainIframe = document.getElementById('mainFormIframeInPermanent');
        $(mainIframe).load(function(){
            var iContent = mainIframe.contentWindow; 
            console.log('d.params.formMsg', d.params.formMsg)
            if(d.params.formMsg && d.params.formMsg.length > 0){
                var formContanier = iContent.document.querySelector('.select-nodes-list ul');
                var chooseNodes = d.params.formMsg;
                var lihtml = '';
                if(chooseNodes && chooseNodes.length > 0){
                    for(var i = 0; i < chooseNodes.length; i++){
                        if(i == 0){
                            lihtml += "<li class='liactive' id='"+chooseNodes[i].id+"' data-model='normal' data-tablename='"+chooseNodes[i].tablename+"' data-formid='"+chooseNodes[i].form_id+"' data-id='"+chooseNodes[i].id+"' data-html='"+chooseNodes[i].html+"'><span>"+chooseNodes[i].formname+"</span><i class='layui-icon layui-icon-close-fill T-btn' data-target='del-form'></i></li>"
                        }else{
                            lihtml += "<li data-model='normal' id='"+chooseNodes[i].id+"' data-tablename='"+chooseNodes[i].tablename+"' data-formid='"+chooseNodes[i].form_id+"' data-id='"+chooseNodes[i].id+"' data-html='"+chooseNodes[i].html+"'><span>"+chooseNodes[i].formname+"</span><i class='layui-icon layui-icon-close-fill T-btn' data-target='del-form'></i></li>"
                        }
                    }
                    formContanier.innerHTML = lihtml;
                    var firstLi = formContanier.querySelector('li');
                    firstLi.click();
                }
            }

            //表单预览
            $('#mainFormIframeInPermanent').contents().find('#preview').click(function(){
                var template = document.createElement('div'),
                    selectNode = iContent.document.querySelector('.liactive'),
                    formContent = iContent.leipiEditor.getContent();
                template.innerHTML = formContent;
                if(formContent){
                    var	cols = iContent.leipiFormDesign.getCols(template);
                    layer.open({
                        id: 'formView',
                        title:'<i class="formFill-icon"></i><span style="color:#FFF;">表单预览</span>',
                        offset: '50px',
                        type: 1,
                        shadeClose: true,
                        area: ['1250px', '550px'],
                        success: function(lay){
                            view(this.id).render('./fill/components/formView',{cols: cols, formname: selectNode.innerText});
                        }
                    });
                }else{
                    layer.msg('请选择表单预览', {time: 1500});
                }
            })
        })
        
        //页面按钮点击事件
        $('.permanent-updateHtml-content').on('click', '.T-event', function() { 
            var $container = $('.permanent-updateHtml-content'),
                target = $(this).data('target'); 
              
            if(target === 'add') {  
                function getResultArr(originArr, selectArr) {
                    var resultArr = [];
                    $.each(originArr,function(index, item) {
                        var isShowOut = false;
                        $.each(selectArr, function(i, e) {
                            if(item.dept_id === e.dept_id) {
                                item.model = 'normal';
                                resultArr.push(item);
                                isShowOut = true;
                            }
                        });
                        if(!isShowOut) {
                            item.model = 'del';
                            resultArr.push(item);
                        }
                    });
                    $.each(selectArr,function(index, item) {
                        var isShowInner = false;
                        $.each(originArr, function(i, e) {
                            if(item.dept_id === e.dept_id) {
                                isShowInner = true;
                            }
                        });
                        if(!isShowInner) {
                            item.model = 'insert';
                            item.taskid = d.params.id || '';
                            item.dataid = item.dept_id;
                            resultArr.push(item);
                        }
                    });
                    return resultArr;
                }
                //表单信息
                var formMsg = [],
				    myIframe = document.getElementById('mainFormIframeInPermanent'),
                    selectNodes = myIframe.contentWindow.document.querySelectorAll('.select-nodes-list ul li'),
                    reviewMan = [],
                    reviewApply = $('.review-value-box span'); 
                    $.each(selectNodes, function(index, item){
						item.click();
						var model = item.getAttribute('data-model'),
							html = item.getAttribute('data-html'), 
                            params = myIframe.contentWindow.reloadTab(html, 'copy'); 
						if(model == 'insert'){
							params.tablename = '';
							params.form_id = '';
							params.id = '';
						}else if(model == 'choose' || model == 'normal'){
							params.model = model;
							params.tablename = item.getAttribute('data-tablename');
                            params.form_id = item.getAttribute('data-formid');  
							params.id = model == 'normal' ? item.getAttribute('id') : '';
						}
						formMsg.push(params);
                    });

                if(formMsg.length === 0) {
                    layer.msg('请先选择表单');
                    return false;
                };

                //复核人信息
				if(reviewApply){
					$.each(reviewApply, function(index, item){
						reviewMan.push({ user_code: item.getAttribute('usercode'), userName: item.innerText, cert_code: item.getAttribute('certcode') })
					})
				}
                
                var resultDep = getResultArr(originDep, selectDep); 
                var $form = $('.permanent-updateHtml-content .permanent-frequency-form');
                var arr = tools.getAreaArgs($form.find('.dateType .date-item'), {type: 'array'});
                var args = tools.getAreaArgs($('.permanent-updateHtml-form'), {input: 1});
                if(!args.taskname) {
                    layer.msg('请先填写任务名称');
                    return false;
                }
                var hz = $('.frequencyType').find('input[name="frequencyType"]:checked').val();
                var settype = $form.find('input[name="timeType"]:checked').val() === 'frequency' ? 0 : 1;
                var isReview = '1';
                var timeDate = [];
                var flag = false;
                $.each(arr, function(index, item) {
                    if(!!item.starttime && !!item.endtime) {
                        item.starttime = moment(item.starttime).format('YYYY-MM-DD HH:mm:ss');
                        item.endtime = moment(item.endtime).format('YYYY-MM-DD HH:mm:ss');
                        item.hz = hz;
                        item.settype = settype;
                        timeDate.push(item);
                    }
                });
                if(timeDate.length === 0) {
                    layer.msg('请先选择至少一组开始结束时间');
                    return false;
                }
                if(resultDep){
                    layui.each(resultDep, function(index, item){
                        if(item.model != 'del' ){
                            flag = true;
                            return ;
                        }
                    })
                } 
                args.fileNames = JSON.stringify(filenames);
                args.timeDate = JSON.stringify(timeDate);
                args.deptMsg = JSON.stringify(resultDep);
                args.formMsg = JSON.stringify(formMsg);
                args.wadduserMsg = JSON.stringify(reviewMan);
                args.review = isReview;
                args.model = 'insert';
                args.updatedata = '0';
                args.tasktype = 'cz';
                args.username = '';
                args.userid = '';
                args.filename = '';
                args.servername = '';
                args.deptname = '';
                args.deptid = '';
                args.jsonId = layui.data('layuiAdmin').access_token;
                if(!!d.params.id) {
                    args.model = 'update';
                    args.updatedata = '1';
                }
                if(!flag){ layer.msg('请先选择下发单位'); return false; }
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/Resident',
                    type: 'post',
                    data: args,
                    success: function(res){
                        layer.msg(res.data.retmsg);
                        if(res.data.retcode == '0'){
                            layer.closeAll('page');
                            window.selectTable = [];
                            tools.destroyIframe(myIframe);
                            d.params.callBack && d.params.callBack();
                        }
                    }
                });
            } else if (target === 'unit') {
                var permanentUrl = "./task/temporary/past"
                layer.open({
                    id: 'permanent-add-pastHtml',
                    title:'<i class="formFill-icon"></i><span style="color:#FFF;">任务下发</span>',
                    offset: '20px',
                    type: 1,
                    shadeClose: true,
                    area: ['450px', '650px'],
                    success: function(lay, index) {
                        view(this.id).render(permanentUrl, {
                            exitDep: selectDep
                        }).done(function() {
                            $(lay).on('click', '.T-confirm', function() {
                                selectDep = JSON.parse($(lay).find('.data-dep-container').val());
                                var depNameStr = selectDep;
                                var getTpl = permanentselectunitTpl.innerHTML;
                                var $tplC =$('.permanent-update-department');
                                laytpl(getTpl).render(depNameStr, function(html){
                                    $tplC.html(html);
                                });
                                layer.close(index);
                            });
                        });
                    }
                });
            } else if (target === 'delUnit') {
                var depid = $(this).data('deptid'); 
                var filterDep = [];
                $.each(selectDep, function(index) {
                    if(selectDep[index].dept_id !== depid) {
                        filterDep.push(selectDep[index]);
                    }
                });
                selectDep = filterDep;
                var depNameStr = selectDep;
                var getTpl = permanentselectunitTpl.innerHTML;
                var $tplC =$('.permanent-update-department');
                laytpl(getTpl).render(depNameStr, function(html){
                    $tplC.html(html);
                });
            } else if (target === 'delFile') {
                var depid = $(this).data('deptid');
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/Resident/delfile',
                    type: 'post',
                    data: {servername: depid},
                    success: function(res){
                        if(res.retcode == 0) {
                            var filterFiles = [];
                            $.each(filenames, function(index) {
                                if(filenames[index].servername !== depid) {
                                    filterFiles.push(filenames[index]);
                                }
                            });
                            filenames = filterFiles;
                            var getTpl = permanentuploadFilesTpl.innerHTML;
                            var $tplC =$('.permanent-update-files');
                            laytpl(getTpl).render(filenames, function(html){
                                $tplC.html(html);
                            });
                        }
                    }
                });
            } else if (target === 'viewlast') {
                var $form = $('.permanent-updateHtml-content .permanent-frequency-form');
                var arr = tools.getAreaArgs($form.find('.dateType .date-item'), {type: 'array'}, function($obj) {
                    var starttime = $obj.find('.item-time-start').val(),
                        endtime = $obj.find('.item-time-end').val();
                    return !!starttime && !!endtime;
                });
                var hz = $('.frequencyType').find('input[name="frequencyType"]:checked').val();
                var timeDate = [];
                $.each(arr, function(index, item) {
                    item.starttime = moment(item.starttime).format('YYYY-MM-DD HH:mm:ss');
                    item.endtime = moment(item.endtime).format('YYYY-MM-DD HH:mm:ss');
                    timeDate.push(item);
                })
                var args = {
                    hz: hz,
                    timeDate: timeDate
                }
                if(timeDate.length === 0) {
                    layer.msg('请先选择至少一组开始结束时间');
                    return false;
                }
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/getthelatesttime',
                    type: 'post',
                    data: {time: JSON.stringify(args)},
                    success: function(res){
                        var getTpl = permanentShowLastTpl.innerHTML;
                        var $tplC =$('.permanent-frequency-form .listType .body');
                        laytpl(getTpl).render(res.data, function(html){
                            $tplC.html(html);
                        });
                    }
                });
            }else if (target === 'download') {
                var url = 'myapi/DC/'+ tools.subUrl($(this).data('url')), 
                    name = $(this).text();
                tools.downMethodfunction(url, name);
            }else if(target == 'to-review'){
				var text = $(this).siblings('label').text(),
                    reviewUrl = null,
                    selectPerson = $('.review-value-box span'),
                    selectIds = [];
                if(switchKey == 0){
                    return false
                }else{
                    if(text == '附加复核人：'){
                        reviewUrl = './task/temporary/addPerson'
                    }else{
                        reviewUrl = './task/temporary/pullPerson'
                    }
                    if(selectPerson){
                        layui.each(selectPerson, function(index, item){
							selectIds.push({ 
								dept_name: item.getAttribute('deptname'),
								userName: item.innerText,
								user_code: item.getAttribute('usercode'), 
								phone: item.getAttribute('phone'),
								email: item.getAttribute('email'),
								cert_code: item.getAttribute('certcode')
							});
						})
                    }
                    layer.open({
                        id: 'temporary-reviewHtml',
                        title: '<i class="formFill-icon"></i><span style="color:#FFF;">'+text+'</span>',
                        type: 1,
                        offset: '20px',
                        shadeClose: true,
                        area: ['570px', '650px'],
                        success: function(lay, index){
                            view(this.id).render(reviewUrl, {
                                checkIds: selectIds || []
                            }).done(function() {
                                $(lay).on('click', '.T-confirm', function() {
                                    if(text == "附加复核人："){
                                        var selectReview = $(lay).find('.check-value-box .unit-content'),
												$eachman = '';
										$.each(selectReview, function(index, item){
											var usercode = item.querySelector('.del-tag').getAttribute('data-usercode'),
												certcode = item.querySelector('.del-tag').getAttribute('data-certcode'),
												deptname = item.querySelector('.del-tag').getAttribute('data-deptnme'),
												phone = item.querySelector('.del-tag').getAttribute('data-phone'),
												email = item.querySelector('.del-tag').getAttribute('data-email'),
												username = item.querySelector('a').innerText;
											$eachman += '<span usercode="'+usercode+'" certcode="'+certcode+'" deptname="'+deptname+'" phone="'+phone+'" email="'+email+'">'+username+'</span>、';
										})
										$('.review-value-box').html($eachman);
                                    }else{
                                        var selectAudit = JSON.parse($(lay).find('.data-audit-container').val());
                                        $('.audit-value-box').html(JSON.stringify(selectAudit.join(',')).replace(/"/g, ''));
                                    }
                                    layer.close(index);
                                });
                            });
                        }
                    })
                }
			}
        })
    });
}

</script>